﻿#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>

struct S
{
	char name[20];
	int age;
	float score;
};
#if 0
int main()
{
	struct S s = { "张三",26,65.5f };
	//想把s中的数据存放在文件中
	FILE* pf = fopen("text.txt", "w");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//写文件 - 是以文本的形式写进去的
	fprintf(pf, " %s %d, %f", s.name, s.age, s.score);
	
	fclose(pf);
	pf = NULL;

	return 0;
}
#endif

#if 0
int main()
{
	struct S s = { "张三",26,65.5f };
	//想从文件text.txt中读取数据放在s中
	FILE* pf = fopen("text.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//读文件
	fscanf(pf, "%s %d %f", s.name, &(s.age), &(s.score));

	//打印在屏幕上看看
	printf("%s %d %f\n", s.name, s.age, s.score);

	return 0;
}
#endif


//函数对比
/*
* scanf - 从标准输入流上读取格式化数据
* fscanf - 从指定的输入流上读取格式化数据
* sscanf - 在字符串中读取格式化数据
* 
* printf - 把数据以格式化的形式打印在标准输出流上
* fprintf - 把数据以格式化的形式打印在指定的输出流上
* sprintf - 把格式化的数据转化成字符串

*/
#if 0
int main()
{
	char buf[200] = { 0 };
	struct S s = { "张三",26,65.5f };
	sprintf(buf, "%s %d %f", s.name, s.age, s.score);
	puts(buf);//1

	struct S t = { 0 };
	sscanf(buf, "%s %d %f", t.name, &(t.age), &(t.score));
	printf("%s %d %f\n", t.name, t.age, t.score);//2

	return 0;
}
#endif


#if 0
int main()
{
	int arr[] = { 1,2,3,4,5 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	FILE* pf = fopen("text.txt", "wb");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//写数据
	fwrite(arr, sizeof(arr[0]), sz, pf);//以二进制的形式写入

	fclose(pf);
	pf = NULL;

	return 0;
}
#endif


#if 0
#include<stdlib.h>

int main()
{
	int arr[5] = { 0 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	FILE* pf = fopen("text.txt", "rb");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//读数据-以二进制的形式读取
	int ret = fread(arr, sizeof(arr[0]), 5, pf);
	for (int i = 0; i < 5; i++)
	{
		printf("%d ", arr[i]);//1 2 3 4 5
	}

	printf("\n%d\n", ret);

	fclose(pf);
	pf = NULL;

	return 0;
}
#endif 


# if 0
int main()
{
	int arr[5] = { 0 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	FILE* pf = fopen("text.txt", "rb");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//读数据-以二进制的形式读取
	int i = 0;
	int ret = 0;
	while(fread(arr+i, sizeof(int), 1, pf))
	{
		printf("%d ", arr[i++]);
	}

	fclose(pf);
	pf = NULL;

	return 0;
}
#endif



//fseek(FILE* stream, int offset, int origin);

#if 0
int main()
{
	FILE* pf = fopen("text.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	int ch = getc(pf);
	printf("%c\n", ch);//a

	//fseek(pf, 6, SEEK_CUR);
	//fseek(pf, 7, SEEK_SET);
	fseek(pf, -2, SEEK_END);

	ch = getc(pf);
	printf("%c\n", ch);//h

	fclose(pf);
	pf = NULL;

	return 0;
}
#endif


//long int ftell(FILE* stream)

# if 0
int main()
{
	FILE* pf = fopen("text.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//读文件
	int ch = fgetc(pf);
	printf("%c\n", ch);

	fseek(pf, -2, SEEK_END);

	printf("%ld\n", ftell(pf));

	fclose(pf);
	pf = NULL;

	return 0;
}
#endif

//void rewind(FLIE* stream)

#if 0
int main()
{
	FILE* pf = fopen("text.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	//读文件
	int ch = fgetc(pf);
	printf("%c\n", ch);

	fseek(pf, -2, SEEK_END);
	printf("%ld\n", ftell(pf));
	fseek(pf, -2, SEEK_CUR);
	printf("%ld\n", ftell(pf));

	rewind(pf);

	printf("%ld\n", ftell(pf));

	fclose(pf);
	pf = NULL;

	return 0;
}
#endif 

#if 0
int main()
{
	FILE* pf = fopen("text.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	int ch = 0;
	while ((ch = fgetc(pf)) != EOF)
	{
		printf("%c\n", ch);
	}
	//判断是什么原因导致文件结束的
	if (feof(pf))
	{
		printf("遇到文件末尾，读取正常结束\n");
	}
	else if (ferror(pf))
	{
		perror("fgetc");
	}

	fclose(pf);
	pf = NULL;

	return 0;
}
#endif


#if 0
int main()
{
	FILE* pf = fopen("text.txt", "r");
	if (pf == NULL)
	{
		perror("fopen");
		return 1;
	}
	char ch = 'a';
	for (ch = 'a'; ch <= 'z'; ch++)
	{
		fputc(ch, pf);
	}
	//判断是什么原因导致文件结束的
	if (feof(pf))
	{
		printf("遇到文件末尾，读取正常结束\n");
	}
	else if (ferror(pf))
	{
		perror("fputc");
	}

	fclose(pf);
	pf = NULL;

	return 0;
}
#endif






#if 0
#include <stdio.h>
#include <windows.h>
//VS2019 WIN11环境测试
int main()
{
	FILE * pf = fopen("data.txt", "w");
	fputs("abcdef", pf);//先将代码放在输出缓冲区
	printf("睡眠10秒-已经写数据了，打开data.txt文件，发现文件没有内容\n");
	Sleep(10000);
	printf("刷新缓冲区\n");
	fflush(pf);//刷新缓冲区时，才将输出缓冲区的数据写到⽂件（磁盘）
	//注：fflush 在高版本的VS上不能使⽤了
	printf("再睡眠10秒-此时，再次打开data.txt文件，文件有内容了\n");
	Sleep(10000);
	fclose(pf);
	//注：fclose在关闭文件的时候，也会刷新缓冲区
	pf = NULL;
	
	return 0;
}
#endif

